﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.BLL;
using SIS.ENT;

public partial class teacher_PhotoUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();

            if (Request.Params["id"] != null)
            {

                int _accountId = 0;
                if (Int32.TryParse(Request.Params["id"].ToString(), out _accountId))
                {
                    LoadTeacherInfo(_accountId);
                }
                else
                {
                    lblUserAccountKey.Text = "-1";
                }


            }


        }
    }

    private void LoadTeacherInfo(int AccountID)
    {
        UserAccountTable _account = new UserAccountTable();
        _account = UserAccountTableManager.GetItem(AccountID);

        //if (_account == null)
        //{
        //    lblTopMessage.Text = "Teacher account ID (" + AccountID.ToString() + ") is not found!";
        //}
        //else if (_account.UserAccountKey <= 0)
        //{
        //    lblTopMessage.Text = "Teacher account ID is not found!";
        //}

        if (_account != null && _account.UserAccountKey > 0)
        {
            //if (_account.UserAccountTypeCode != 200)
            //{
            //    lblTopMessage.Text = "Only for Teacher account ID will be loaded.";
            //    return;
            //}

            lblUserAccountKey.Text = _account.UserAccountKey.ToString();

            TeacherProfileTable _sprofile = new TeacherProfileTable();
            TeacherDesignationTable _tdesignation = new TeacherDesignationTable();
            //TeacherAddressTable _sraddress = new TeacherAddressTable();
            //TeacherAddressTable _spaddress = new TeacherAddressTable();
            //TeacherJobExperiencesTableList _sJobExperiencesList = new TeacherJobExperiencesTableList();
            //TeacherDesignationTableList _sTeacherDesignationList = new TeacherDesignationTableList();
            //TeacherSalaryTableList _sTeacherSalaryList = new TeacherSalaryTableList();
            //TeacherQualtificationTableList _sQualificationList = new TeacherQualtificationTableList();
            //TeacherOtherSkillsTableList _sSkillList = new TeacherOtherSkillsTableList();
            //TeacherContactTable _scontact = new TeacherContactTable();

            _sprofile = TeacherProfileTableManager.GetItemByUserAccountKey(AccountID, _account.SchoolKey);
            if (_sprofile != null && _sprofile.TeacherProfileKey > 0)
            {
                lblTeacherProfileKey.Text = _sprofile.TeacherProfileKey.ToString();

                string _fullName = string.Empty;

                _fullName = _sprofile.FirstName;

                if (_fullName.Length == 0)
                {
                    _fullName = _sprofile.LastName;
                }
                else
                {
                    _fullName += " " + _sprofile.LastName;
                }

                if (_sprofile.OtherName != null && _sprofile.OtherName.Length > 0)
                {
                    _fullName = _sprofile.OtherName;
                }

                lblFullName.Text = _fullName;

                if (_sprofile.Photo != null && _sprofile.Photo.Length > 0)
                {
                    imgCurrentTeacherPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + lblTeacherProfileKey.Text + "&type=teacherphoto";
                    hplCurrentTeacherPhoto.NavigateUrl = imgCurrentTeacherPhoto.ImageUrl;
                    imgCurrentTeacherPhoto.Width = 100;
                    imgCurrentTeacherPhoto.Height = 150;
                }
                else
                {
                    imgCurrentTeacherPhoto.ImageUrl = "";
                    hplCurrentTeacherPhoto.NavigateUrl = "";
                    imgCurrentTeacherPhoto.Width = 0;
                    imgCurrentTeacherPhoto.Height = 0;
                }

                _tdesignation = TeacherDesignationTableManager.GetActiveDesignationItem(_sprofile.UserAccountKey);
                if (_tdesignation != null)
                {
                    JobDesignationCodeTable _jdcode = new JobDesignationCodeTable();
                    _jdcode = JobDesignationCodeTableManager.GetItem(_tdesignation.JobDesignationCode);
                    if (_jdcode != null)
                    {
                        lblCurrentDesignation.Text = _jdcode.Description;
                        lblCurrentDesignation.Text += "<br/><br/>Job Desc:<br/>" + _tdesignation.JobDescriptionDetail;
                    }
                }
            }


            //Contact Table

            //_scontact = TeacherContactTableManager.GetItemByUserAccountKey(AccountID);

            //if (_scontact != null && _scontact.TeacherContactKey > 0)
            //{
            //    lblTeacherContactKey.Text = _scontact.TeacherContactKey.ToString();
            //    txtMobileNumber.Text = _scontact.MobileNumber;
            //    txtHomePhoneNumber.Text = _scontact.HomePhoneNumber;
            //    txtOtherContactNumber.Text = _scontact.OtherNumber;
            //    txtEmailAddress.Text = _scontact.EmailAddress;
            //}
            //Resident
            //_sraddress = TeacherAddressTableManager.GetItemByUserAccountKey(AccountID, 1);
            //if (_sraddress != null && _sraddress.TeacherAddressKey > 0)
            //{
            //    lblTeacherRAddressKey.Text = _sraddress.TeacherAddressKey.ToString();
            //    txtUnitNo.Text = _sraddress.UnitNo;
            //    txtStreetName.Text = _sraddress.StreetName;
            //    ddlCountry.SelectedValue = _sraddress.CountryCode.ToString();
            //    _common.LoadDistrictCodeDDL(ddlDistrict, Convert.ToInt16(ddlCountry.SelectedItem.Value), string.Empty);
            //    ddlDistrict.SelectedValue = _sraddress.DistrictCode.ToString();
            //    _common.LoadCityCodeDDL(ddlCity, Convert.ToInt32(ddlDistrict.SelectedValue), Convert.ToInt16(ddlCountry.SelectedValue), string.Empty);
            //    ddlCity.SelectedValue = _sraddress.CityCode.ToString();
            //    txtPostCode.Text = _sraddress.PostalCode;
            //}

            ////Permanent/Contact
            //_spaddress = TeacherAddressTableManager.GetItemByUserAccountKey(AccountID, 2);
            //if (_spaddress != null && _spaddress.TeacherAddressKey > 0)
            //{
            //    lblTeacherPAddressKey.Text = _spaddress.TeacherAddressKey.ToString();
            //    txtPUnitNo.Text = _spaddress.UnitNo;
            //    txtPStreetName.Text = _spaddress.StreetName;
            //    ddlPCountry.SelectedValue = _spaddress.CountryCode.ToString();
            //    _common.LoadDistrictCodeDDL(ddlPDistrict, Convert.ToInt16(ddlPCountry.SelectedItem.Value), string.Empty);
            //    ddlPDistrict.SelectedValue = _spaddress.DistrictCode.ToString();
            //    _common.LoadCityCodeDDL(ddlPCity, Convert.ToInt32(ddlPDistrict.SelectedValue), Convert.ToInt16(ddlCountry.SelectedValue), string.Empty);
            //    ddlPCity.SelectedValue = _spaddress.CityCode.ToString();
            //    txtPPostCode.Text = _spaddress.PostalCode;
            //    //chkPSameAsResident.Checked = _spaddress.SameAsResident;
            //    chkPSameAsResident_CheckedChanged(null, null);
            //}

            ////Job Experiences
            ////Job Experiences
            //DataTable lDT = new DataTable();
            //_sJobExperiencesList = new TeacherJobExperiencesTableList();
            //Int32 ProfileKey = _sprofile.TeacherProfileKey;
            //_sJobExperiencesList = TeacherJobExperiencesTableManager.GetList(AccountID);
            //CreateTable(_sJobExperiencesList);
            //dgExperienceBrowse.DataSource = Session["JobExperiencesDT"];
            //dgExperienceBrowse.DataBind();


            ////Designation
            //DataTable lDesignationDT = new DataTable();
            //_sTeacherDesignationList = new TeacherDesignationTableList();
            //_sTeacherDesignationList = TeacherDesignationTableManager.GetList(AccountID);
            //CreateDesignationTable(_sTeacherDesignationList);
            //gvDesignation.DataSource = Session["TeacherDesignationsDT"];
            //gvDesignation.DataBind();

            ////Salary
            //DataTable lSalaryDT = new DataTable();
            //_sTeacherSalaryList = new TeacherSalaryTableList();
            //_sTeacherSalaryList = TeacherSalaryTableManager.GetList(AccountID);
            //CreateSalaryTable(_sTeacherSalaryList);
            //gvSalary.DataSource = Session["TeacherSalaryDT"];
            //gvSalary.DataBind();

            ////Designation
            //DataTable lQualificationDT = new DataTable();
            //_sQualificationList = new TeacherQualtificationTableList();
            //_sQualificationList = TeacherQualtificationTableManager.GetList(AccountID);
            //CreateQualifyTable(_sQualificationList);
            //dgvTeacherQualtification.DataSource = Session["QualificationDT"];
            //dgvTeacherQualtification.DataBind();

            ////Designation
            //DataTable lSkillDT = new DataTable();
            //_sSkillList = new TeacherOtherSkillsTableList();
            //_sSkillList = TeacherOtherSkillsTableManager.GetList(AccountID);
            //CreateSkillTable(_sSkillList);
            //gvTeacherOtherSkills.DataSource = Session["TeacherOtherSkillsDT"];
            //gvTeacherOtherSkills.DataBind();

        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Upload failed!";

        int _fileLen = 0;
        string _fileName = string.Empty;
        string _fileExt = string.Empty;

        bool _fileExtValid = false;

        string[] _arrFileExt = System.Configuration.ConfigurationManager.AppSettings["SchoolImageUploadFileExt"].Split(',');

        int _PhotoFileSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["StudentUploadLogoFileSizeKB"]);


        if (fupPhoto.HasFile && fupPhoto.PostedFile != null)
        {
            if (fupPhoto.PostedFile.ContentLength > 0)
            {
                _fileLen = fupPhoto.PostedFile.ContentLength / 1024;
            }

            _fileName = fupPhoto.PostedFile.FileName;
            _fileExt = System.IO.Path.GetExtension(_fileName).ToLower();

            for (int i = 0; i < _arrFileExt.Length; i++)
            {
                if (_fileExt == _arrFileExt[i])
                {
                    _fileExtValid = true;
                    break;
                }
            }

            if (_fileExtValid)
            {
                if (_fileLen > 0 && _fileLen <= _PhotoFileSize)
                {
                    Byte[] imgPhoto = null;
                    //To create a PostedFile
                    HttpPostedFile File = fupPhoto.PostedFile;
                    //Create byte Array with file len
                    imgPhoto = new Byte[File.ContentLength];
                    //force the control to load data in array
                    File.InputStream.Read(imgPhoto, 0, File.ContentLength);

                    if (TeacherProfileTableManager.UpdatePhoto(Convert.ToInt32(lblTeacherProfileKey.Text), imgPhoto))
                    {
                        lblMessage.Text = "<br/>Update photo succeeded!";
                        imgCurrentTeacherPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + lblTeacherProfileKey.Text + "&type=teacherphoto";
                        hplCurrentTeacherPhoto.NavigateUrl = imgCurrentTeacherPhoto.ImageUrl;
                        imgCurrentTeacherPhoto.Width = 100;
                        imgCurrentTeacherPhoto.Height = 150;
                    }
                    else
                    {
                        lblMessage.Text = "<br/>Update photo failed!";
                    }
                    imgPhoto = null;
                    File = null;
                }
                else
                {
                    lblMessage.Text = "<br/>Photo size is over limit!";
                }
            }
            else
            {
                lblMessage.Text += "<br/>Photo file type is NOT allowed to update!";
            }
        }
        else
        {
            lblMessage.Text = "No photo file was uploaded!";
        }
    }

    protected void btnViewSchoolProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("profile.aspx");
    }

    protected void btnViewTeacherProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("teacherentry.aspx?id=" + lblUserAccountKey.Text);
    }
}
